package com.ruoyi.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.PaeaConstants;
import com.ruoyi.project.para.paeaParaCalendar.domain.PaeaParaCalendar;
import com.ruoyi.project.para.paeaParaCalendar.service.IPaeaParaCalendarService;
import com.ruoyi.project.param.paeaParaSystem.domain.PaeaParaSystem;
import com.ruoyi.project.param.paeaParaSystem.enums.ArchiveServerType;
import com.ruoyi.project.param.paeaParaSystem.service.IPaeaParaSystemService;
import com.ruoyi.project.system.config.domain.Config;
import com.ruoyi.project.system.config.service.IConfigService;
import com.ruoyi.project.system.dept.service.IDeptService;
import com.ruoyi.project.system.parawork.domain.PaeaTaskLimitPara;
import com.ruoyi.project.system.parawork.service.IPaeaTaskLimitParaService;
import com.ruoyi.project.system.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

/**
 * 功能描述：
 *
 * @author 关晓强
 * @version 1.0
 * @date 2020/3/18 21:20
 */
@Slf4j
@Configuration
public class CommonParaTool implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private IPaeaParaSystemService paeaParaSystemService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IPaeaParaCalendarService paeaParaCalendarService;

    @Autowired
    private IPaeaTaskLimitParaService paeaTaskLimitParaService;

    @Autowired
    private static IPaeaParaCalendarService staticPaeaParaCalendarService;

    @Autowired
    private static IPaeaTaskLimitParaService staticPaeaTaskLimitParaService;

    @Autowired
    private IConfigService configService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        initBean();
        initPaeaParaSystem();
        initDept();
        initUser();
        initPaeaPaeaCalender();
        initSysConfig();
    }

    @PostConstruct
    private void initBean(){
        staticPaeaParaCalendarService = paeaParaCalendarService;
        staticPaeaTaskLimitParaService = paeaTaskLimitParaService;
    }
    private void initPaeaParaSystem() {
        List<PaeaParaSystem> paeaParaSystems = paeaParaSystemService.list();
        for (PaeaParaSystem paeaParaSystem : paeaParaSystems) {
            if (paeaParaSystem.getReserved1() != null
                    && paeaParaSystem.getReserved1().trim().length() > 0) {
                PaeaConstants.PAEA_PARA_SYSTEM_MAP.put(paeaParaSystem.getReserved1().trim(),
                        paeaParaSystem);
                if (paeaParaSystem.getReserved2() != null) {
                    PaeaConstants.PAEA_PARA_SYSTEM_MAP.put(paeaParaSystem.getReserved1().trim()
                                    + "-" + paeaParaSystem.getReserved2().trim(),
                            paeaParaSystem);
                }
            }
        }
    }

    private void initDept() {
        PaeaConstants.DEPT_LIST = deptService.selectAllDepts();
    }
    private void initUser() {
        PaeaConstants.USER_LIST = userService.selectAllUsers();
    }

    private void initPaeaPaeaCalender() {
        PaeaConstants.CALENDAR_LIST = paeaParaCalendarService.list(new QueryWrapper<PaeaParaCalendar>().orderByAsc("code"));
    }

    private void initSysConfig() {
         List<Config> configs = configService.selectConfigList(new Config());
         for (Config config: configs){
             PaeaConstants.SYS_CONFIG_MAP.put(config.getConfigKey(),config);
         }
    }
    //获取文档服务器前缀
    public static String getImgPathPrefix(String auditOrgCode) {

        PaeaParaSystem paeaParaSystem = getPaeaParaSystemByOrgCode(auditOrgCode, ArchiveServerType.查询专用.getCode());
        String serverIp = paeaParaSystem.getFileHttpIp();
        int serverPort = paeaParaSystem.getFileHttpPort();

        return serverIp + ":" + serverPort + "/"+ paeaParaSystem.getFileHttpServerlet();
    }

    //todo 改为缓存
    public static PaeaParaSystem getPaeaParaSystemByOrgCode(String orgCode) {
        if (orgCode == null || orgCode.trim().length() == 0) {
            throw new RuntimeException("没有监控中心号");
        }
        // 根据监控中心去拿文档服务器
        PaeaParaSystem paeaParaSystem = PaeaConstants.PAEA_PARA_SYSTEM_MAP.get(orgCode);
        if (paeaParaSystem == null) {
            throw new RuntimeException("监控中心" + orgCode + "未配置文档服务器");
        } else {
            return paeaParaSystem;
        }
    }

    //todo 改为缓存
    public static PaeaParaSystem getPaeaParaSystemByOrgCode(String orgCode,
                                                            String archiveServerType) {
        if (orgCode == null || orgCode.trim().length() == 0) {
            throw new RuntimeException("没有监控中心号");
        }
        PaeaParaSystem paeaParaSystem = PaeaConstants.PAEA_PARA_SYSTEM_MAP.get(orgCode + "-"
                + archiveServerType);
        if (paeaParaSystem == null) {
            paeaParaSystem = PaeaConstants.PAEA_PARA_SYSTEM_MAP.get(orgCode + "-"
                    + ArchiveServerType.通用.getCode());
            if (paeaParaSystem == null) {
                return getPaeaParaSystemByOrgCode(orgCode);
            }
        }
        return paeaParaSystem;
    }

    /**
     * 检查code是否节假日
     *
     * @param code
     *            true 是节假日 false 不是节假日
     * @return
     */
    public static boolean checkIsHoliday(String code) {
        // 为空认为不是节假日，返回false
        if (code == null || "".equals(code)) {
            return false;
        }
        for (PaeaParaCalendar paeaParaCalendar : PaeaConstants.CALENDAR_LIST) {
            if (code.equals(paeaParaCalendar.getValue())) {
                return true;
            }
        }
        // 工作日
        return false;
    }

    /**
     * 获取风险限期整改日期
     * @return
     */
    public static Date getCorrectLimitDate() {
        int limit = retrieveLimitPara();
        return staticPaeaParaCalendarService.findLimitDateWorkDay(limit, new java.sql.Date(System.currentTimeMillis()));
    }

    /**
     * 返回风险限期整改参数
     * @return
     */
    private static int retrieveLimitPara() {
        int limit = 1;
        PaeaTaskLimitPara paeaTaskLimitPara = new PaeaTaskLimitPara();
        paeaTaskLimitPara.setCode(PaeaConstants.ERROR_LIMIT);
        List<PaeaTaskLimitPara> paeaTaskLimitParas = staticPaeaTaskLimitParaService.selectPaeaTaskLimitParaList(paeaTaskLimitPara);
        if(StringUtils.isNotEmpty(paeaTaskLimitParas)){
            PaeaTaskLimitPara para = paeaTaskLimitParas.get(0);
            limit = para.getLimit();
        }
        return limit;
    }
}
